<template>
  <div class="chat-container">
    <!-- 左侧联系人列表 -->
    <div class="contact-list">
      <div class="list-header">
        <h3>消息列表</h3>
        <el-input
          placeholder="搜索联系人"
          prefix-icon="Search"
          v-model="searchQuery"
          clearable
          class="search-input"
        />
      </div>
      <div class="contacts">
        <div
          v-for="(contact, index) in filteredContacts"
          :key="index"
          class="contact-item"
          :class="{ 'active': currentContact.id === contact.id }"
          @click="selectContact(contact)"
        >
          <el-avatar :size="40" :src="contact.avatar">{{ contact.name.charAt(0) }}</el-avatar>
          <div class="contact-info">
            <div class="contact-name">{{ contact.name }}</div>
            <div class="last-message">{{ contact.lastMessage }}</div>
          </div>
          <div class="contact-time">{{ contact.lastTime }}</div>
        </div>
      </div>
    </div>

    <!-- 右侧聊天区域 -->
    <div class="chat-area">
      <!-- 聊天头部 -->
      <div class="chat-header">
        <div class="chat-title">
          <span>{{ currentContact.name }}</span>
        </div>
        <div class="chat-actions">
          <el-tooltip content="通话" placement="bottom">
            <el-icon class="action-icon"><Phone /></el-icon>
          </el-tooltip>
          <el-tooltip content="视频" placement="bottom">
            <el-icon class="action-icon"><VideoCamera /></el-icon>
          </el-tooltip>
          <el-tooltip content="更多" placement="bottom">
            <el-icon class="action-icon"><MoreFilled /></el-icon>
          </el-tooltip>
        </div>
      </div>

      <!-- 消息区域 -->
      <div class="message-area" ref="messageArea">
        <div v-for="(message, index) in currentMessages" :key="index"
          class="message"
          :class="{ 'message-self': message.isSelf }">
          <el-avatar
            v-if="!message.isSelf"
            :size="36"
            :src="currentContact.avatar"
          >{{ currentContact.name.charAt(0) }}</el-avatar>
          <div class="message-content" :class="{ 'self-content': message.isSelf }">
            {{ message.content }}
          </div>
          <el-avatar
            v-if="message.isSelf"
            :size="36"
            :src="userAvatar"
          >我</el-avatar>
        </div>
      </div>

      <!-- 输入区域 -->
      <div class="input-area">
        <div class="toolbar">
          <el-tooltip content="表情" placement="top">
            <el-icon class="tool-icon"><Emoji /></el-icon>
          </el-tooltip>
          <el-tooltip content="图片" placement="top">
            <el-icon class="tool-icon"><Picture /></el-icon>
          </el-tooltip>
          <el-tooltip content="文件" placement="top">
            <el-icon class="tool-icon"><Folder /></el-icon>
          </el-tooltip>
          <el-tooltip content="剪刀" placement="top">
            <el-icon class="tool-icon"><Scissor /></el-icon>
          </el-tooltip>
          <el-tooltip content="添加" placement="top">
            <el-icon class="tool-icon"><CirclePlus /></el-icon>
          </el-tooltip>
        </div>
        <div class="input-box">
          <el-input
            v-model="messageInput"
            type="textarea"
            :rows="3"
            placeholder="发送给 陈秋"
            resize="none"
            @keyup.enter.native.prevent="sendMessage"
          />
        </div>
        <div class="send-area">
          <el-tooltip content="全屏" placement="top">
            <el-icon class="tool-icon"><FullScreen /></el-icon>
          </el-tooltip>
          <el-button type="primary" @click="sendMessage" :disabled="!messageInput.trim()">
            <el-icon><ArrowRight /></el-icon>
            发送
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, nextTick } from 'vue'
import {
  Search,
  Phone,
  VideoCamera,
  MoreFilled,
  Picture,
  Folder,
  Scissor,
  CirclePlus,
  FullScreen,
  ArrowRight
} from '@element-plus/icons-vue'

interface Contact {
  id: number
  name: string
  avatar: string
  lastMessage: string
  lastTime: string
  unread?: number
}

interface Message {
  content: string
  time: string
  isSelf: boolean
}

// 用户头像
const userAvatar = ref('')

// 联系人列表
const contacts = ref<Contact[]>([
  {
    id: 1,
    name: '陈秋',
    avatar: '',
    lastMessage: '请问我的检查结果怎么样？',
    lastTime: '10:30',
    unread: 2
  },
  {
    id: 2,
    name: '李明',
    avatar: '',
    lastMessage: '好的，谢谢医生',
    lastTime: '昨天',
  },
  {
    id: 3,
    name: '张华',
    avatar: '',
    lastMessage: '我最近感觉头晕，是怎么回事？',
    lastTime: '周一',
  },
  {
    id: 4,
    name: '王芳',
    avatar: '',
    lastMessage: '下次复诊是什么时候？',
    lastTime: '上周',
  },
])

// 当前选中的联系人
const currentContact = ref<Contact>(contacts.value[0])

// 搜索查询
const searchQuery = ref('')

// 过滤后的联系人列表
const filteredContacts = computed(() => {
  if (!searchQuery.value) return contacts.value
  return contacts.value.filter(contact =>
    contact.name.toLowerCase().includes(searchQuery.value.toLowerCase())
  )
})

// 消息输入
const messageInput = ref('')

// 消息区域引用，用于滚动
const messageArea = ref<HTMLElement | null>(null)

// 消息列表
const messages = ref<Record<number, Message[]>>({
  1: [
    { content: '您好，医生', time: '10:25', isSelf: false },
    { content: '您好，有什么可以帮助您的？', time: '10:26', isSelf: true },
    { content: '请问我的检查结果怎么样？', time: '10:30', isSelf: false },
  ],
  2: [
    { content: '医生，我的药吃完了', time: '昨天', isSelf: false },
    { content: '您可以再来复诊一次，我们再开药', time: '昨天', isSelf: true },
    { content: '好的，谢谢医生', time: '昨天', isSelf: false },
  ],
  3: [
    { content: '我最近感觉头晕，是怎么回事？', time: '周一', isSelf: false },
  ],
  4: [
    { content: '下次复诊是什么时候？', time: '上周', isSelf: false },
  ],
})

// 当前联系人的消息
const currentMessages = computed(() => {
  return messages.value[currentContact.value.id] || []
})

// 选择联系人
const selectContact = (contact: Contact) => {
  currentContact.value = contact
  // 清除未读消息
  if (contact.unread) {
    contact.unread = 0
  }
  // 滚动到底部
  scrollToBottom()
}

// 发送消息
const sendMessage = () => {
  if (!messageInput.value.trim()) return

  const newMessage: Message = {
    content: messageInput.value,
    time: new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
    isSelf: true
  }

  if (!messages.value[currentContact.value.id]) {
    messages.value[currentContact.value.id] = []
  }

  messages.value[currentContact.value.id].push(newMessage)

  // 更新最后一条消息
  currentContact.value.lastMessage = messageInput.value
  currentContact.value.lastTime = newMessage.time

  // 清空输入
  messageInput.value = ''

  // 滚动到底部
  nextTick(() => {
    scrollToBottom()
  })
}

// 滚动到底部
const scrollToBottom = () => {
  nextTick(() => {
    if (messageArea.value) {
      messageArea.value.scrollTop = messageArea.value.scrollHeight
    }
  })
}

// 组件挂载后滚动到底部
onMounted(() => {
  scrollToBottom()
})
</script>

<style scoped lang="scss">
.chat-container {
  display: flex;
  height: calc(100vh - 60px);
  background-color: #f5f7fa;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 左侧联系人列表样式 */
.contact-list {
  width: 280px;
  background-color: #fff;
  border-right: 1px solid #e6e6e6;
  display: flex;
  flex-direction: column;
}

.list-header {
  padding: 16px;
  border-bottom: 1px solid #e6e6e6;

  h3 {
    margin: 0 0 12px 0;
    font-size: 16px;
    font-weight: 500;
  }

  .search-input {
    margin-top: 8px;
  }
}

.contacts {
  flex: 1;
  overflow-y: auto;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  position: relative;

  &:hover {
    background-color: #f5f7fa;
  }

  &.active {
    background-color: #ecf5ff;
  }

  .contact-info {
    flex: 1;
    margin-left: 12px;
    overflow: hidden;
  }

  .contact-name {
    font-weight: 500;
    margin-bottom: 4px;
  }

  .last-message {
    font-size: 12px;
    color: #909399;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .contact-time {
    font-size: 12px;
    color: #909399;
    margin-left: 8px;
  }
}

/* 右侧聊天区域样式 */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;

  .chat-title {
    font-size: 16px;
    font-weight: 500;
  }

  .chat-actions {
    display: flex;
    gap: 16px;

    .action-icon {
      font-size: 20px;
      color: #606266;
      cursor: pointer;

      &:hover {
        color: #409eff;
      }
    }
  }
}

.message-area {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}

.message {
  display: flex;
  margin-bottom: 16px;
  align-items: flex-start;

  &.message-self {
    flex-direction: row-reverse;
  }

  .message-content {
    max-width: 70%;
    padding: 10px 14px;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    margin: 0 12px;
    word-break: break-word;

    &.self-content {
      background-color: #ecf5ff;
      color: #303133;
    }
  }
}

.input-area {
  background-color: #fff;
  border-top: 1px solid #e6e6e6;
  padding: 10px 20px;
}

.toolbar {
  display: flex;
  gap: 16px;
  padding: 8px 0;

  .tool-icon {
    font-size: 20px;
    color: #606266;
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}

.input-box {
  margin: 10px 0;

  :deep(.el-textarea__inner) {
    resize: none;
    border-radius: 4px;
    padding: 8px 12px;
    min-height: 80px;
  }
}

.send-area {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .el-button {
    display: flex;
    align-items: center;
    gap: 4px;
  }
}
</style>
